'''

Case Type： 文件目录
Case Name： 主设备pg_log目录无权限时业务正常运行
Create At:  2020/07/27
Owner    :  @li-xin12345
Descption:  1.	执行TPCC业务2.	在业务执行过程中修改pg_log权限

history：
created： 2020/07/27  created by @li-xin12345
'''
import os
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread

logger = Logger()


class ModifyPgxlogPermi(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby_1_sh = CommonSH('Standby1DbUser')
    standby_2_sh = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    primaryRoot = Node(node='PrimaryRoot')
    primaryNode = Node(node='PrimaryDbUser')
    dbStandby1DbUser = Node(node='Standby1DbUser')
    dbStandby2DbUser = Node(node='Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case185.py start------------')
        self.TPCC_PATH = macro.TPCC_PATH
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.com = Common('PrimaryDbUser')
        self.Constant = Constant()

    def test_delpermission_pgxlog(self):
        logger.info('---------------------------开始执行TPCC-------------------------')
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.primaryRoot, self.TPCC_PATH,))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()
        time.sleep(5)

        logger.info('---------------------删除pg_log权限---------------------------')
        modfiyPermisCmd = 'chmod -R 000 ' + self.PG_LOG_PATH
        logger.info(modfiyPermisCmd)
        modifyMsg = self.primaryRoot.sh(modfiyPermisCmd).result()
        logger.info(modifyMsg)

        showPermissionCmd = 'ls -al  ' + self.PG_LOG_PATH.rsplit("/", 1)[0]
        showMsg = self.primaryRoot.sh(showPermissionCmd).result()
        logger.info(showMsg)

        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        logger.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

        # 校验刷脏页是否完成
        logger.info('校验刷脏页是否完成')
        dirty_page_finshed_flag = self.primary_sh.check_whether_remain_dirty_page()
        self.assertTrue(dirty_page_finshed_flag)

        # 校验备机是否完成数据同步
        logger.info('校验备机是否完成数据同步')
        flag_1 = self.standby_1_sh.check_data_consistency()
        self.assertTrue(flag_1)
        flag_2 = self.standby_1_sh.check_data_consistency()
        self.assertTrue(flag_2)

        nodes_tuple = (self.primaryNode, self.dbStandby1DbUser, self.dbStandby2DbUser)
        logger.info("-----------check data consistency-----------")
        flag = self.com.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag)

        table_dict = self.com.format_sql_result(self.standby_1_sh.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.com.check_data_sample_by_all(select_sql, *nodes_tuple)
            self.assertTrue(flag)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        logger.info('---------------------------恢复pg_log权限-------------------------')
        modfiyPermisCmd = 'chmod -R 700 ' + self.PG_LOG_PATH
        modifyMsg = self.primaryRoot.sh(modfiyPermisCmd).result()
        logger.info(modifyMsg)

        logger.info('----------------------------reliability_dbsys185执行完成-----------------------------')
